<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入搜索内容" v-model="goodsInfo.query" @clear="getGoodsList" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
           <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表表格区域 -->
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" label="id"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600px"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="220px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"
            @click="showEditGoods(scope.row.goods_id )">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id )">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页导航区域 -->、
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsInfo.pagenum"
        :page-sizes="[2, 5, 10]"
        :page-size="goodsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data(){
    return{
      // 商品列表数据
      goodslist:[],
      // 查询参数
      goodsInfo:{
        query:'',
        // 当前页码值
        pagenum: 1,
        // 每页数量
        pagesize: 10
      },
      // 商品总条数
      totalNum:0,
      // 控制修改商品对话框 展示
       editGoodsVisible:false,
    }
  },
  created(){
    this.getGoodsList();
  },
  methods:{
    // 获取商品列表 方法
    async getGoodsList(){
        const { data :res} = await this.$http.get('goods', { params: this.goodsInfo});
        
        if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.goodslist = res.data.goods;
        this.totalNum = res.data.total;
    },
    // 监听每页显示的数量改变
    handleSizeChange(nweSize){
      this.goodsInfo.pagesize = nweSize;
      this.getGoodsList();
    },
    // 监听页码值的改变
    handleCurrentChange(newPage){
      this.goodsInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 展示编辑商品对话框 方法
    showEditGoods(id){},
    // 删除商品信息 方法
    async removeGoods(id){
      // 提示用户是否确认删除
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).catch(err => err);
        // 如果用户确认删除,返回字符串confirm
        // 如果用户取消删除,返回字符串cancel
        // 判断用户是否真的删除
        if(confirmResult !== 'confirm'){
          return this.$message.info("已取消删除");
        }
        const {data: res} = await this.$http.delete("goods/" + id);
        if(res.meta.status !== 200){
          return this.$message.error(res.meta.msg);
        }
        this.$message.success("成功删除")
        this.getGoodsList();
    },
    // 添加商品 页面跳转 方法
    async addGoods(){
      this.$router.push("/goods/addgoods");
    }
  }
}


</script>

<style>

</style>